package com.chuangyang.fixboxmaster.ui.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chuangyang.fixboxmaster.R;

/**
 * Created by Administrator on 2015/4/10.
 */
public class CashRequestFragment extends BaseFragment implements View.OnClickListener ,TextView.OnEditorActionListener{

    private View mContentView;
    private static final int VIEW_ID_CASH_INPUT = 1;
    private static final int VIEW_ID_CASH_CONFIRM = 2;
    private int mCurrentViewID = VIEW_ID_CASH_INPUT;
    private TextView mCardNum;// 绑定卡号.
    private TextView mCardAdress;// 开户行
    private TextView mTotalCash;// 账户余额
    private TextView mUsableCash;// 可提现余额
    private EditText mRequestCashInput;// 申请提现金额
    private TextView mRequestCashTitle;
    private TextView mAccountName;// 姓名
    private TextView mRequestCash;//确认提款金额
    private TextView mBalanceCash;// 提现后剩余金额
    private TextView mReceiveDate;// 到帐日
    private Button mConfirmBtn;
    private LinearLayout mCashInputView;
    private LinearLayout mCashConfirmView;
    private CashInfo cashInfo;
    public static CashRequestFragment newInstance(){
        return new CashRequestFragment();
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mContentView = inflater.inflate(R.layout.fragment_cash_request,container,false);
        initView();
        initData();
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setContentView(mContentView);
        showResult();
        showView(VIEW_ID_CASH_INPUT);
    }

    private void initView(){
        mCardNum = (TextView)mContentView.findViewById(R.id.bank_card_num);
        mCardAdress = (TextView)mContentView.findViewById(R.id.bank_card_address);
        mTotalCash = (TextView)mContentView.findViewById(R.id.total_cash);
        mUsableCash = (TextView)mContentView.findViewById(R.id.usable_cash);
        mCashInputView = (LinearLayout)mContentView.findViewById(R.id.request_cash_input_view);
        mRequestCashTitle = (TextView)mContentView.findViewById(R.id.request_cash_title);
        mRequestCashInput = (EditText)mContentView.findViewById(R.id.request_cash_input);
        mCashConfirmView = (LinearLayout)mContentView.findViewById(R.id.request_cash_confirm_view);
        mAccountName = (TextView)mContentView.findViewById(R.id.account_name);
        mRequestCash = (TextView)mContentView.findViewById(R.id.request_cash_confirm);
        mBalanceCash = (TextView)mContentView.findViewById(R.id.balance_cash);
        mReceiveDate = (TextView)mContentView.findViewById(R.id.receive_date);
        mConfirmBtn = (Button)mContentView.findViewById(R.id.confirm);
        mRequestCashInput.setOnEditorActionListener(this);
        mConfirmBtn.setOnClickListener(this);
    }

    private void showView(int viewID){
        mCurrentViewID = VIEW_ID_CASH_INPUT;
        mCashInputView.setVisibility(View.GONE);
        mCashConfirmView.setVisibility(View.GONE);
        if (viewID == VIEW_ID_CASH_INPUT)
        {
            mCashInputView.setVisibility(View.VISIBLE);
            mCashConfirmView.setVisibility(View.GONE);
            mRequestCashTitle.setText("提现金额");
            mConfirmBtn.setText("下一步");
        }
        else if (viewID == VIEW_ID_CASH_CONFIRM)
        {
            hideInputMethod();
            cashInfo.balanceCash = "" + (Integer.parseInt(cashInfo.totalCash) - Integer.parseInt(cashInfo.requestCash) );
            mCashInputView.setVisibility(View.GONE);
            mCashConfirmView.setVisibility(View.VISIBLE);
            mRequestCashTitle.setText("提现信息");
            mConfirmBtn.setText("确认提款");
        }
        loadData();
    }

    private boolean validateCash(){
        cashInfo.requestCash = mRequestCashInput.getText().toString().trim();
        if(TextUtils.isEmpty(cashInfo.requestCash)) {
            mRequestCashInput.setError("金额不能为空!");
            return false;
        }else if(cashInfo.requestCash.length()>9){
            mRequestCashInput.setError("输入金额过大,请重新输入!");
            return false;
        }
        else
        {
            try{
                // 换算成整数判断
                int nRequestCash = Integer.parseInt(cashInfo.requestCash);
                int nUsableCash = Integer.parseInt(cashInfo.usableCash);
                if(nRequestCash == 0){
                    mRequestCashInput.setError("金额不能为零!");
                    return  false;
                }
                else if(nRequestCash%100 != 0){
                    mRequestCashInput.setError("提款金额必须为100的整数倍!");
                    return  false;

                }
                else if(nRequestCash > nUsableCash){
                    mRequestCashInput.setError("金额不能超过可提现金额!");
                    return  false;
                }

            }catch (NumberFormatException e){

                mRequestCashInput.setError("请输入纯数字!");
                return  false;
            }
        }
        return true;
    }


    private void confirmBtnClick(){
        if (mCurrentViewID == VIEW_ID_CASH_INPUT)
        {
            if(validateCash())
            {
                showView(VIEW_ID_CASH_CONFIRM);
            }
        }
        else if (mCurrentViewID == VIEW_ID_CASH_CONFIRM)
        {
            submit();
        }
    }

    private void initData(){ // todo test use
        cashInfo = new CashInfo();
        cashInfo.cardNum = "6222************0210";
        cashInfo.cardAdd = "七宝支行";
        cashInfo.totalCash = "8566";
        cashInfo.usableCash = "3500";
        cashInfo.requestCash = "";
        cashInfo.balanceCash = "";
        cashInfo.accountName = "张大招";
        cashInfo.receiveTime = "4月12日 24:00";
    }
    private void loadData(){ // todo test use
        mCardNum.setText(cashInfo.cardNum);
        mCardAdress.setText(cashInfo.cardAdd);
        mTotalCash.setText(getCashFormatString(cashInfo.totalCash));
        mUsableCash.setText(getCashFormatString(cashInfo.usableCash));
        mRequestCash.setText(getCashFormatString(cashInfo.requestCash));
        mBalanceCash.setText(getCashFormatString(cashInfo.balanceCash));
        mAccountName.setText(cashInfo.accountName);
//        mReceiveDate.setText(cashInfo.receiveTime+"之前");
        mReceiveDate.setText("下周之前");
    }

    private String getCashFormatString(String cash){
        String result = "";
        if (TextUtils.isEmpty(cash))
        {
            return result;
        }
        try{
            result ="￥" + cash;
        }catch (Exception e){
        }
        return result;
    }

    /**
     * 手动关闭键盘
     */
    private void hideInputMethod() {
        InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(mRequestCashInput.getWindowToken(), 0);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.confirm:
                confirmBtnClick();
                break;
            default:
                break;
        }
    }

    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (v == mRequestCashInput) {
            switch (actionId) {
                case EditorInfo.IME_ACTION_DONE:
                    confirmBtnClick();
                    return true;
            }
        }
        return false;
    }

    private class CashInfo{ // todo test class
        private String cardNum;
        private String cardAdd;
        private String totalCash;
        private String usableCash;
        private String balanceCash;
        private String accountName;
        private String requestCash;
        private String receiveTime;
    }

    private void submit(){

    }
}
